const baseUrl="https://oa.itany.com/exchange";
const urls = {
    findAllLoop:"/loop/findAllLoop",
    findArticleList:"/articlelist/findArticleList",
    findAllType:"/type/findAllType",
    findDetail:"/articlelist/findAllArticle",
    users:"/users",
    fileUploadUrl:"/fileUpload/uploadImg",
    addProductUrl:"/articlelist/addArticle"
};
const header = {
    "content-type": "application/x-www-form-urlencoded"
};

function initSuccess({statusCode,data}){
    if(statusCode==200){
        return data;
    }else{
        return Promise.reject("请求失败");
    }
}

function loadLoopList1(){
    return new Promise(function(resolve,reject){
        wx.request({
            url: baseUrl+urls.findAllLoop,
            method: 'POST',
            header,
            success:resolve,
            fail:reject
        });
    }).then(initSuccess);
}

function loadProductList1(params={page:1,rows:4}){
    return new Promise(function(resolve,reject){
        wx.request({
            url: baseUrl+urls.findArticleList,
            method: 'POST',
            data:params,
            header,
            success:resolve,
            fail:reject
        });
    }).then(initSuccess);
}

function loadTypeList(){
    return new Promise(function(resolve,reject){
        wx.request({
            url: baseUrl+urls.findAllType,
            header,
            method: 'POST',
            success:resolve,
            fail:reject,
        });
    }).then(initSuccess);
}
function loadProductById(params){
    return new Promise(function(resolve,reject){
        if(params&&params.id){
            wx.request({
                url: baseUrl+urls.findDetail,
                data: params,
                header,
                method: 'POST',
                success:resolve,
                fail:reject,
            });
        }else{
            reject({
                message:"参数错误"
            })
        }
    }).then(initSuccess);
}

function errorFunction(error,success){
    wx.showModal({
        title: '数据加载错误',
        content: error.message+':请联系管理员',
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: success
    });
}

function findUserByOpenId(openId){
    return new Promise(function(resolve,reject){
        if(openId){
            wx.request({
                url: baseUrl+urls.users+"/"+openId,
                header,
                method: 'GET',
                success:resolve,
                fail:reject
            });
        }else{
            reject({
                message:"参数错误"
            });
        }
    }).then(initSuccess);
}

function addUser(user){
    return new Promise(function(resolve,reject){
        wx.request({
            url: baseUrl+urls.users,
            data: user,
            header,
            method: 'POST',
            success:resolve,
            fail:reject,
        });
    }).then(initSuccess);
}

function uploadFile(path){
    return new Promise(function(resolve,reject){
        wx.uploadFile({
            url: baseUrl+urls.fileUploadUrl,
            filePath: path,
            name: "img",
            success:resolve,
            fail:reject
        });
    })
    .then(initSuccess)
    .then(function(data){
        //返回的是一张图片的url，为JSON字符串 将每个图片
        return JSON.parse(data)
    })
}

function addProduct(params){
    return new Promise(function(resolve,reject){
        wx.request({
            url: baseUrl+urls.addProductUrl,
            data:params,
            header,
            method: 'POST',
            success:resolve,
            fail:reject,
        });
    }).then(initSuccess);
}
module.exports={
    loadLoopList1,
    loadProductList1,
    loadTypeList,
    loadProductById,
    errorFunction,
    findUserByOpenId,
    addUser,
    uploadFile,
    addProduct
}